package monkeyTalk;

import java.io.File;
import java.io.FileInputStream;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Locale;

import jxl.Sheet;
import jxl.Workbook;
import jxl.WorkbookSettings;

import org.junit.Test;

public class CreateObjectRepository extends BaseClass {

  @Test
  public void generateSharedORMAP() throws Exception {
    try {
      log("info", "Started Generating Shared OR MAP...");
      log("info", "Current user dir:" + System.getProperty("user.dir"));
      log("info", "Current Home dir:" + System.getProperty("user.home"));
      String sharedORExcelPath = System.getProperty("user.dir") + "/ObjectRepository/OR_SHARED.xls";
      // String sharedORExcelPath = "../../../ObjectRepository/OR_SHARED.xls";

      FileInputStream fs = null;
      WorkbookSettings ws = null;
      fs = new FileInputStream(new File(sharedORExcelPath));
      ws = new WorkbookSettings();
      ws.setLocale(new Locale("en", "EN"));
      Workbook SharedORworkbook = Workbook.getWorkbook(fs, ws);
      Sheet sharedORSheet = SharedORworkbook.getSheet(0);

      for (int k = 0; k < sharedORSheet.getRows(); k++) {
        String ORKey = sharedORSheet.getCell(1, k).getContents();
        String ORValue = sharedORSheet.getCell(4, k).getContents();
        sharedORMAP.put(ORKey, ORValue);
      }

      fs.close();

      log("info", "Completed Generating Shared OR MAP.(Total items=" + sharedORSheet.getRows()
          + ")");
    } catch (Exception e) {

      log("error", "Exception caught while Generating Shared OR MAP.(" + e.getMessage() + ")");
    }

  }

  public void createLogFile(String testName) throws Exception {

    if (writeLogToFile) {

      String tmpArr[] = testName.split("Test_");
      SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy-hh-mm-ss");
      logFileName =
          "log_" + tmpArr[tmpArr.length - 1] + "_" + sdf.format(System.currentTimeMillis())
              + ".txt";
      PrintWriter writer = new PrintWriter("./logs/" + logFileName, "UTF-8");
      writer.close();
      log("info", "Log file created successfully !{" + "logs/" + logFileName + "}");
    }

  }


}
